from starlette.endpoints import HTTPEndpoint
from starlette.responses import JSONResponse
from model.ModPendingProSupSta import PendingProSupSta, pendingProSupStaSchema
from model.ModPendingProDet import PendingProDet, pendingProDetSchema
from database import Session, engine
import datetime
from sqlalchemy.sql import text
import pendulum
import logging


def update_pending_info(session, bureauid: str, department_level: str):
    # 一键督办
    try:
        updateinfo = session.query(PendingProSupSta).filter(PendingProSupSta.bureauid == bureauid).all()
        txt_rep = 'update "PendingProSupSta" set "max_supervision_no"="max_supervision_no"+1 where bureauid=' + "'" + bureauid + "'"
        session.execute(txt_rep)
        if department_level == "1":
            txt1 = 'update "PendingProSupSta" set "monitor_time"=CURRENT_TIMESTAMP where bureauid=' + "'" + bureauid + "'"
            session.execute(txt1)
            updateinfo.monitor_time = pendulum.now()
            txt = 'update "PendingProDet" set "monitorNo"="monitorNo"+1 where bureauid=' + "'" + bureauid + "'"
            session.execute(txt)
        elif department_level == "2":
            txt1 = 'update "PendingProSupSta" set "director_time"=CURRENT_TIMESTAMP where bureauid=' + "'" + bureauid + "'"
            session.execute(txt1)
            txt = 'update "PendingProDet" set "directorNo"="directorNo"+1 where bureauid=' + "'" + bureauid + "'"
            session.execute(txt)
        elif department_level == "3":
            txt1 = 'update "PendingProSupSta" set "leader_time"=CURRENT_TIMESTAMP where bureauid=' + "'" + bureauid + "'"
            session.execute(txt1)
            txt = 'update "PendingProDet" set "leaderNo"="leaderNo"+1 where bureauid=' + "'" + bureauid + "'"
            session.execute(txt)
        else:
            txt1 = 'update "PendingProSupSta" set "operation_time"=CURRENT_TIMESTAMP where bureauid=' + "'" + bureauid + "'"
            session.execute(txt1)
            txt = 'update "PendingProDet" set "operationNo"="operationNo"+1 where bureauid=' + "'" + bureauid + "'"
            session.execute(txt)
    except Exception as e:
        raise


def get_pending_list(session, bureauid: str):
    # 查询供电所督办列表
    connection = engine.raw_connection()
    cursor = connection.cursor()
    cursor.callproc("statics_pending_pro_stu_sta")
    cursor.close()
    connection.commit()
    pending_list = session.query(PendingProSupSta).filter(PendingProSupSta.bureauid == bureauid).order_by(
        PendingProSupSta.pending_number.desc()).all()
    return JSONResponse(pendingProSupStaSchema.dump(pending_list, many=True).data)


def update_pending_info_station(session, bureauid: str, stationid: str, department_level: str):
    # 按供电所督办
    try:
        updateinfo = session.query(PendingProSupSta).filter(PendingProSupSta.bureauid == bureauid).filter(
            PendingProSupSta.stationid == stationid).one_or_none()
        updateinfo.max_supervision_no += 1
        up_det = session.query(PendingProDet).filter(PendingProDet.bureauid == bureauid).filter(
            PendingProDet.stationid == stationid).all()
        if department_level == "1":
            updateinfo.monitor_time = pendulum.now()
            txt = 'update "PendingProDet" set "monitorNo"="monitorNo"+1 where bureauid=' + "'" + bureauid + "'" + ' and stationid=' + "'" + stationid + "'"
            session.execute(txt)
        elif department_level == "2":
            updateinfo.director_time = pendulum.now()
            txt = 'update "PendingProDet" set "directorNo"="directorNo"+1 where bureauid=' + "'" + bureauid + "'" + ' and stationid=' + "'" + stationid + "'"
            session.execute(txt)
        elif department_level == "3":
            updateinfo.leader_time = pendulum.now()
            txt = 'update "PendingProDet" set "leaderNo"="leaderNo"+1 where bureauid=' + "'" + bureauid + "'" + ' and stationid=' + "'" + stationid + "'"
            session.execute(txt)
        else:
            updateinfo.operation_time = pendulum.now()
            txt = 'update "PendingProDet" set "operationNo"="operationNo"+1 where bureauid=' + "'" + bureauid + "'" + ' and stationid=' + "'" + stationid + "'"
            session.execute(txt)
    except Exception as e:
        raise


def get_check(session, bureauid: str, stationid: str):
    # 查询供电所是否督办过
    pending_list = session.query(PendingProSupSta).filter(PendingProSupSta.bureauid == bureauid).filter(
        PendingProSupSta.stationid == stationid).one_or_none()
    info = pendingProSupStaSchema.dump(pending_list, many=False).data
    today_com = pendulum.now().strftime('%Y-%m-%d')
    director_time = ''
    leader_time = ''
    monitor_time = ''
    if info["director_time"] is not None:
        director_time = info["director_time"][0:10]
    if info["leader_time"] is not None:
        leader_time = info["leader_time"][0:10]
    if info["monitor_time"] is not None:
        monitor_time = info["monitor_time"][0:10]
    if (today_com == director_time) or (today_com == leader_time) or (today_com == monitor_time):
        return "2"
    else:
        return "1"

def get_check_operation(session, bureauid: str, stationid: str):
    # 查询供电所是否督办过采集运维
    pending_list = session.query(PendingProSupSta).filter(PendingProSupSta.bureauid == bureauid).filter(
        PendingProSupSta.stationid == stationid).one_or_none()
    info = pendingProSupStaSchema.dump(pending_list, many=False).data
    today_com = pendulum.now().strftime('%Y-%m-%d')
    operation_time = ''
    if info["operation_time"] is not None:
        operation_time = info["operation_time"][0:10]
    if today_com == operation_time:
        return "2"
    else:
        return "1"
